package com.arelikebrothers.demo.concurrent.jdk;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by lennon on 01/05/2017.
 * Condition和ReentrantLock搭配使用
 */
public class ReentrantLockCondition implements Runnable {
    public static ReentrantLock lock = new ReentrantLock();
    // Condition和ReentrantLock一般都是搭配使用，用来实现wait()和notify()类似的功能。
    public static Condition condition = lock.newCondition();

    @Override
    public void run() {
        try {
            lock.lock();
            condition.await();
            System.out.println("Thread is going on.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReentrantLockCondition c = new ReentrantLockCondition();
        Thread t1 = new Thread(c);
        t1.start();

        Thread.sleep(2000);

        // 通知线程t1继续执行
        lock.lock();
        condition.signal();
        lock.unlock();

    }
}
